#!/bin/bash
clear
echo -e "\e[1;35m=============================================================\e[0m"
echo 
echo -e "                       \e[1;32m脚本由CentOS6.7测试通过\e[0m"
echo -e "                       \e[1;32m一键部署LAMP+phpMyAdmin\e[0m"
echo -e "                                         \e[1;32mby LuSao\e[0m"
echo 
echo -e "\e[1;35m=============================================================\e[0m"
myip=`wget http://ipecho.net/plain -O - -q ; echo`
echo -e "\e[1;31m[你的IP是:$myip]\e[0m"
#!/bin/bash
sleep 5
echo -e "\e[1;44m请输入MySQL密码\e[0m"
read -p "(回车将设置默认密码:root):" SQ
[ -z "$SQ" ] && SQ="root"
        echo 
        echo -e "---------------------------"
        echo -e "已设置MySQL密码为 = $SQ"
        echo -e "---------------------------"
        echo 
echo -e "\e[1;44m请输入网页编码\e[0m"
read -p "(回车将设置默认密码:UTF-8):" BM
[ -z "$BM" ] && BM="UTF-8"
        echo 
        echo -e "---------------------------"
        echo -e "已设置网页编码为 = $BM"
        echo -e "---------------------------"
        echo 
read -p "回车开始部署吧!"
echo -e "\e[1;31m已开始部署，请耐心等待...\e[0m"
sleep 5
clear
cd /root/
unalias cp >/dev/null 2>&1
echo -e "\e[1;31m正在配置防火墙...(防火墙已关闭请忽略此步骤)\e[0m"
iptables -A INPUT -p tcp --dport 3306 -j ACCEPT >/dev/null 2>&1
iptables -A INPUT -p tcp --dport 80 -j ACCEPT >/dev/null 2>&1
service iptables save
service iptables restart
echo -e "\e[1;31m正在整理旧环境...(可能需要30秒的时间，请勿回车!)\e[0m"
rm -rf /var/lib/mysql/mysql >/dev/null 2>&1
yum remove -y httpd >/dev/null 2>&1
yum remove -y mysql mysql-server >/dev/null 2>&1
yum remove -y php >/dev/null 2>&1
yum remove -y php-mysql php-gd libjpeg* php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-bcmath php-mhash libmcrypt >/dev/null 2>&1
cd /var/www/html >/dev/null 2>&1
rm -rf phpmyadmin >/dev/null 2>&1
cd /root
echo -e "\e[1;31m正在安装新环境...\e[0m"
yum install -y wget unzip
yum install -y httpd
service httpd restart
chkconfig httpd on
yum install -y mysql mysql-server
service mysqld restart
chkconfig mysqld on
rm -rf /etc/my.cnf
cp -rf /usr/share/mysql/my-medium.cnf /etc/my.cnf
yum install -y php
service httpd restart
yum install -y php-mysql php-gd libjpeg* php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-bcmath php-mhash libmcrypt
service httpd restart
service mysqld restart
cd /etc/httpd/conf
sed -i "s/ServerTokens OS/ServerTokens Prod/g" httpd.conf >/dev/null 2>&1
sed -i "s/ServerSignature On/ServerSignature Off/g" httpd.conf >/dev/null 2>&1
sed -i "s/Options Indexes FollowSymLinks/Options Includes ExecCGI FollowSymLinks/g" httpd.conf >/dev/null 2>&1
sed -i "s/#AddHandler cgi-script .cgi/AddHandler cgi-script .cgi .pl/g" httpd.conf >/dev/null 2>&1
sed -i "s/AllowOverride None/AllowOverride All/g" httpd.conf >/dev/null 2>&1
sed -i "s/AddDefaultCharset UTF-8/AddDefaultCharset $BM/g" httpd.conf >/dev/null 2>&1
sed -i "s/Options Indexes MultiViews FollowSymLinks/Options MultiViews FollowSymLinks/g" httpd.conf >/dev/null 2>&1
sed -i "s/DirectoryIndex index.html index.html.var/DirectoryIndex index.html index.htm Default.html Default.htm index.php Default.php index.html.var/g" httpd.conf >/dev/null 2>&1
sed -i "s/KeepAlive Off/KeepAlive On/g" httpd.conf >/dev/null 2>&1
sed -i "s/MaxKeepAliveRequests 100/MaxKeepAliveRequests 1000/g" httpd.conf >/dev/null 2>&1
service httpd restart
rm -rf /etc/httpd/conf.d/welcome.conf /var/www/error/noindex.html
cd /etc
sed -i "s/;date.timezone =/date.timezone = PRC/g" php.ini >/dev/null 2>&1
sed -i "946adisable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru, stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd,posix_getegid,posix_geteuid,posix_getgid,posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid, posix_getppid,posix_getpwnam,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid, posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname" php.ini >/dev/null 2>&1
sed -i "s/expose_php = On/expose_php = Off/g" php.ini >/dev/null 2>&1
sed -i "s/display_errors = ON/display_errors = OFF/g" php.ini >/dev/null 2>&1
sed -i "s/register_globals = ON/register_globals = OFF/g" php.ini >/dev/null 2>&1
sed -i "s/magic_quotes_gpc = Off/magic_quotes_gpc = On/g" php.ini >/dev/null 2>&1
sed -i "s/log_errors = Off/log_errors = On/g" php.ini >/dev/null 2>&1
sed -i "s/error_log = /error_log = \/var\/log\/php\//g" php.ini >/dev/null 2>&1
sed -i "s/open_basedir =/open_basedir = .:\/tmp\//g" php.ini >/dev/null 2>&1
cd /root
wget https://git.oschina.net/lusao/lamp/raw/master/phpMyAdmin-4.0.10.18-all-languages.tar.gz
tar zxvf phpMyAdmin-4.0.10.18-all-languages.tar.gz >/dev/null 2>&1
mv /root/phpMyAdmin-4.0.10.18-all-languages /var/www/html/phpmyadmin
cp -rf /var/www/html/phpmyadmin/config.sample.inc.php /var/www/html/phpmyadmin/config.inc.php
cd /var/www/html/phpmyadmin
sed -i "42a\$cfg['Servers'][\$i]['controlhost'] = '';" config.inc.php >/dev/null 2>&1
sed -i "43a\$cfg['Servers'][\$i]['controluser'] = 'pma';" config.inc.php >/dev/null 2>&1
sed -i "44a\$cfg['Servers'][\$i]['controlpass'] = 'pmapass';" config.inc.php >/dev/null 2>&1
sed -i "47a\$cfg['Servers'][\$i]['pmadb'] = 'phpmyadmin';" config.inc.php >/dev/null 2>&1
sed -i "48a\$cfg['Servers'][\$i]['bookmarktable'] = 'pma__bookmark';" config.inc.php >/dev/null 2>&1
sed -i "49a\$cfg['Servers'][\$i]['relation'] = 'pma__relation';" config.inc.php >/dev/null 2>&1
sed -i "50a\$cfg['Servers'][\$i]['table_info'] = 'pma__table_info';" config.inc.php >/dev/null 2>&1
sed -i "51a\$cfg['Servers'][\$i]['table_coords'] = 'pma__table_coords';" config.inc.php >/dev/null 2>&1
sed -i "52a\$cfg['Servers'][\$i]['pdf_pages'] = 'pma__pdf_pages';" config.inc.php >/dev/null 2>&1
sed -i "53a\$cfg['Servers'][\$i]['column_info'] = 'pma__column_info';" config.inc.php >/dev/null 2>&1
sed -i "54a\$cfg['Servers'][\$i]['history'] = 'pma__history';" config.inc.php >/dev/null 2>&1
sed -i "55a\$cfg['Servers'][\$i]['table_uiprefs'] = 'pma__table_uiprefs';" config.inc.php >/dev/null 2>&1
sed -i "56a\$cfg['Servers'][\$i]['tracking'] = 'pma__tracking';" config.inc.php >/dev/null 2>&1
sed -i "57a\$cfg['Servers'][\$i]['designer_coords'] = 'pma__designer_coords';" config.inc.php >/dev/null 2>&1
sed -i "58a\$cfg['Servers'][\$i]['userconfig'] = 'pma__userconfig';" config.inc.php >/dev/null 2>&1
sed -i "59a\$cfg['Servers'][\$i]['recent'] = 'pma__recent';" config.inc.php >/dev/null 2>&1
cd /var/www/html/phpmyadmin/libraries
sed -i "185s/= '';/= 'username';/g" config.default.php >/dev/null 2>&1
sed -i "194s/= '';/= 'passwrod';/g" config.default.php >/dev/null 2>&1
sed -i "290s/= '';/= 'phpmyadmin';/g" config.default.php >/dev/null 2>&1
sed -i "299s/= '';/= 'pma__bookmark';/g" config.default.php >/dev/null 2>&1
sed -i "308s/= '';/= 'pma__relation';/g" config.default.php >/dev/null 2>&1
sed -i "317s/= '';/= 'pma__table_info';/g" config.default.php >/dev/null 2>&1
sed -i "326s/= '';/= 'pma__table_coords';/g" config.default.php >/dev/null 2>&1
sed -i "335s/= '';/= 'pma__pdf_pages';/g" config.default.php >/dev/null 2>&1
sed -i "344s/= '';/= 'pma__column_info';/g" config.default.php >/dev/null 2>&1
sed -i "353s/= '';/= 'pma__history';/g" config.default.php >/dev/null 2>&1
sed -i "362s/= '';/= 'pma__designer_coords';/g" config.default.php >/dev/null 2>&1
sed -i "369s/= '';/= 'pma__recent';/g" config.default.php >/dev/null 2>&1
sed -i "376s/= '';/= 'pma__table_uiprefs';/g" config.default.php >/dev/null 2>&1
sed -i "385s/= '';/= 'pma__tracking';/g" config.default.php >/dev/null 2>&1
sed -i "394s/= '';/= 'pma__userconfig';/g" config.default.php >/dev/null 2>&1
rm -rf /root/phpMyAdmin-4.0.10.18-all-languages.tar.gz
rm -rf /root/lamp
echo -e "\e[1;31m此处会提示输入原MySQL密码，请直接回车!\e[0m"
mysqladmin -uroot -p password $SQ
mysql -uroot -p$SQ << EOF 2>/dev/null
create database phpmyadmin
EOF
mysql -uroot -p$SQ phpmyadmin < /var/www/html/phpmyadmin/examples/create_tables.sql
echo -e "\e[1;31m提示:(提示Y请输入Y后回车，提示输入密码请输入你刚刚设置的MySQL密码!)\e[0m"
echo -e "\e[1;31m提示:(new password=输入你的密码 Re-enter new password=重复输入你的密码)\e[0m"
mysql_secure_installation
echo -e "\e[1;31m正在重启MySQL...\e[0m"
service mysqld restart
alias cp='cp i' >/dev/null 2>&1
clear
echo -e "\e[1;35m=============================================================\e[0m"
echo 
echo -e "\e[1;32m           你的网站地址为:$myip\e[0m"
echo -e "\e[1;32m           你的phpMyAdmin地址为:$myip/phpmyadmin\e[0m"
echo -e "\e[1;32m           你的网站目录为/var/www/html/\e[0m"
echo -e "\e[1;32m          ----------------------------------------------\e[0m"
echo -e "          \e[1;32m恭喜LAMP+phpMyAdmin已部署完成，需要使用php\e[0m"
echo -e "          \e[1;32mMyAdmin功能请看使用说明。(使用说明在home目录)\e[0m"
echo -e "                                             \e[1;32mby LuSao\e[0m"
echo 
echo -e "\e[1;35m=============================================================\e[0m"
